﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HexunMicrobloggingClientUI
{
    public partial class NewTaskForm : Form
    {
        public NewTaskForm()
        {
            InitializeComponent();

            #region Initialize MessageBoxManager.

            //mbm.AutoClose = true;
            //mbm.AutoCloseResult = DialogResult.OK;
            //mbm.HookEnabled = true;
            //mbm.CenterWindow = true;

            #endregion
        }

        #region Public fields

        /// <summary>
        /// User name of account.
        /// </summary>
        public string UserName
        {
            get { return tbUserName.Text.Trim(); }
            set { tbUserName.Text = value; }
        }

        /// <summary>
        /// Password of account.
        /// </summary>
        public string Password
        {
            get { return tbPassword.Text; }
            set { tbPassword.Text = value; }
        }

        /// <summary>
        /// The amount of time to delay before callback is invoked, in milliseconds. 
        /// Specify System.Threading.Timeout.Infinite to prevent the timer from starting. 
        /// Specify zero (0) to start the timer immediately. 
        /// </summary>
        public int DelayTime
        {
            get
            {
                if (rbtnStartImmediately.Checked)
                {
                    // Specify zero (0) to start the timer immediately. 
                    // return 0;

                    return 1000;
                }

                if (rbtnDoNotStart.Checked)
                {
                    return System.Threading.Timeout.Infinite;
                }

                return (int)numUpDownDelayForUpTo.Value * 60 * 1000;
            }

            set
            {
                int delayTime = value;
                if (delayTime.Equals(1000))
                {
                    rbtnStartImmediately.Checked = true;
                }
                else if (delayTime.Equals(System.Threading.Timeout.Infinite))
                {
                    rbtnDoNotStart.Checked = true;
                }
                else
                {
                    delayTime = value / 1000 / 60;

                    rbtnDelayForUpTo.Checked = true;
                    numUpDownDelayForUpTo.Value = delayTime;
                }
            }
        }

        /// <summary>
        /// The time interval between invocations of callback, in milliseconds. 
        /// Specify System.Threading.Timeout.Infinite to disable periodic signaling.
        /// </summary>
        public int TimerInterval
        {
            get
            {
                int unit = 60 * 1000;
                if (cbSetInterval.Checked)
                {
                    return (int)numUpDownSetInterval.Value * unit;
                }

                return unit;
            }
            set
            {
                int interval = (int)value / 1000 / 60;
                if (!interval.Equals(1))
                {
                    cbSetInterval.Checked = true;
                }
                numUpDownSetInterval.Value = interval;
            }
        }

        public const int DEFAULT_COUNTER_NUM = 1;
        public const int MAX_COUNTER_NUM = 10000;

        /// <summary>
        /// Counte for time interval.
        /// </summary>
        public int MaxCountNum
        {
            get
            {
                if (cbAccordingToNums.Checked)
                {
                    return (int)numUpDownAccordingToTheNums.Value;
                }

                return DEFAULT_COUNTER_NUM;
            }

            set
            {
                numUpDownAccordingToTheNums.Value = value;
            }
        }

        /// <summary>
        /// Indicate if count the failed task or not.
        /// </summary>
        public bool CountEvenIfFailed
        {
            get
            {
                return countEvenIfFailed;
            }

            set
            {
                countEvenIfFailed = value;
                cbCountEvenIfFailed.Checked = countEvenIfFailed;
            }
        }
        private bool countEvenIfFailed = false;

        public TextSource textSource
        {
            get
            {
                string text = string.Empty;
                EnumTextType enumTextType = EnumTextType.DerectText;

                if (rbtnRSSSource.Checked)
                {
                    text = tbRSSSourceUri.Text.Trim();
                    enumTextType = EnumTextType.RSSUri;
                }
                else if (rbtnRandomText.Checked)
                {
                    text = Guid.NewGuid().ToString();
                }
                else
                {
                    text = tbFixedContent.Text.Trim();
                }
                
                return new TextSource(text, enumTextType);
            }
        }

        #endregion

        #region Event Handlers of Controls

        private bool isUserNameTyped = false;
        private bool isPasswordTyped = false;

        private void tbUserName_TextChanged(object sender, EventArgs e)
        {
            isUserNameTyped = !string.IsNullOrEmpty(UserName);
            isPasswordTyped = !string.IsNullOrEmpty(Password);

            btnTest.Enabled = isUserNameTyped && isPasswordTyped;
            btnOK.Enabled = CanClickOKButton();
        }

        private void tbPassword_TextChanged(object sender, EventArgs e)
        {
            isUserNameTyped = !string.IsNullOrEmpty(UserName);
            isPasswordTyped = !string.IsNullOrEmpty(Password);

            btnTest.Enabled = isUserNameTyped && isPasswordTyped;
            btnOK.Enabled = CanClickOKButton();
        }

        private bool CanClickOKButton()
        {
            bool canClick = false;
            canClick = isUserNameTyped && isPasswordTyped;
            canClick = canClick && (
                rbtnRandomText.Checked || 
                (rbtnRSSSource.Checked && tbRSSSourceUri.Text != string.Empty) || 
                (rbtnFixedContent.Checked && tbFixedContent.Text.Trim() != string.Empty));
            return canClick;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            string content = "Test";
            string responseType;
            string responseContent;

            if (CoreFunc.Send(UserName, Password, content, out responseType, out responseContent, out errorMessage))
            {
                ShowMessageBox("Success", true, 1);
            }
            else
            {
                ShowMessageBox("Failed: " + errorMessage, false, 1);
            }
        }

        private void rbtnRandomText_CheckedChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = CanClickOKButton();
        }

        private void rbtnRSSSource_CheckedChanged(object sender, EventArgs e)
        {
            btnSelectRSSSource.Enabled = rbtnRSSSource.Checked;
            btnOK.Enabled = CanClickOKButton();
        }

        private void rbtnFixedContent_CheckedChanged(object sender, EventArgs e)
        {
            tbFixedContent.Enabled = rbtnFixedContent.Checked;
            btnOK.Enabled = CanClickOKButton();
        }

        private void rbtnDelayForUpTo_CheckedChanged(object sender, EventArgs e)
        {
            numUpDownDelayForUpTo.Enabled = rbtnDelayForUpTo.Checked;
        }

        private void cbAccordingToNums_CheckedChanged(object sender, EventArgs e)
        {
            numUpDownAccordingToTheNums.Enabled = cbAccordingToNums.Checked;
        }

        private void cbSetInterval_CheckedChanged(object sender, EventArgs e)
        {
            numUpDownSetInterval.Enabled = cbSetInterval.Checked;
        }

        private void tbRSSSourceUri_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = CanClickOKButton();
        }

        private void tbFixedContent_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = CanClickOKButton();
        }

        private void btnSelectRSSSource_Click(object sender, EventArgs e)
        {
            SelectRSSSourceForm selectRssForm = new SelectRSSSourceForm();
            if (selectRssForm.ShowDialog() == DialogResult.OK)
            {
                tbRSSSourceUri.Text = selectRssForm.SelectdRssUris;
            }
        }

        #endregion

        #region MessageBoxManager related.

        /// <summary>
        /// Instance of MassageBoxManager.
        /// </summary>
        //private MessageBoxManager mbm = new MessageBoxManager();

        /// <summary>
        /// Show an auto close message box above parent form's center.
        /// </summary>
        /// <param name="text">Message content.</param>
        /// <param name="autoClose">If need auto close.</param>
        /// <param name="timeOutInSeconds">Auto close time out in seconds.</param>
        private void ShowMessageBox(string text, bool autoClose, int timeOutInSeconds)
        {
            //mbm.AutoClose = autoClose;
            //mbm.TimeOut = timeOutInSeconds;
            MessageBox.Show(text);
        }

        #endregion
    }
}
